package edu.mtsu.csci3033;

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.*;


/** Description of TeamProject Table class
 *
 * @author Dan Woodson
 * @author Trevor Powis
 * @author Kristi Brockelsby
 * @author Jared Gailey
 * @version 1: October 26, 2009
 */
public class TeamProject extends Applet implements ActionListener, MouseListener
{
	char[][] charValues = new char[10][10];	    //array of values
	Table aTable;				    //used to create and store values
	boolean allowUserInput = false;		    // boolean for allowing user input
	Button BtnStart;			    // button used to create 5x5 table
	Button randomChangeBtn;			    // button used to change a value in table
	Button startBtn;			    // button used to start the game
	boolean guessMode;			    // mode allowing the user to guess changed value
	private AudioClip[] sounds = new AudioClip[4];    //array to hold audio clips
	private int counter = 0;		    //counter for audio clip array
	String instructions;			    //opening instructions
	//int size = 5;


	public void init()
	{
	  //setup buttons
		BtnStart = new Button("add row/column");
		BtnStart.addActionListener((ActionListener)this);	//cast

		randomChangeBtn = new Button("change one value");  // creates change one value button
		randomChangeBtn.addActionListener((ActionListener)this);  // creates action listener for button

		//create start button
		startBtn = new Button("Start the game!");
		startBtn.addActionListener((ActionListener)this);

  		//add button
		this.add(startBtn);

		//add image to Image objects
		Image imgO = getImage(getCodeBase(), "images/not.gif");		//O image
		Image imgX = getImage(getCodeBase(), "images/cross.gif");	//X image
		Image fly_img = getImage(getCodeBase(), "images/fly.jpg");
	    	//setup table
		aTable = new Table(100, 100, 75, 55, 5, 5, imgX, imgO, fly_img);

		//setBackground(Color.LIGHT_GRAY);
		super.resize(700, 700);
		validate();


		//add mouse listener
		addMouseListener(this);

		//initially guessMode will be false
		guessMode = false;

		//initialize array of audio clips
		sounds[0] = getAudioClip (getCodeBase(), "images/buzzthruloud.wav");	//wrong
		sounds[1] = getAudioClip (getCodeBase(), "images/foghorn.wav");		//wrong
		sounds[2] = getAudioClip (getCodeBase(), "images/whistldn.wav");	//wrong
		sounds[3] = getAudioClip (getCodeBase(), "images/fio-trumpet.wav");	//right

		//the game instructions
		instructions = "Hello, and welcome to Stump the Student!\n" +
                    "To begin the game, click on the \'Start\' button.\n" +
                    "Then click \'add row/column\' to add a row and column\n" +
                    "and then click \'change a value\' to change one of\n" +
                    "the squares at random. See if you can figure out\n" +
                    "which square was changed. Click on the square you think\n" +
                    "is wrong and the computer will tell you if you're right or\n" +
                    "wrong. If you get it right you can on on to the next level!";

		//show the instructions
		JOptionPane.showMessageDialog(null, instructions, "alert", JOptionPane.INFORMATION_MESSAGE);

		repaint();
		validate();

	}

	/** Description of paint(Graphics g)
	 *
	 * Function draws table and sets the table color
	 * @param g	graphics object used to draw table
	 * @return	void
	 */
  	public void paint(Graphics g)
  	{
  		g.setColor(Color.black);
   		aTable.draw(g);
   		validate();
  	}


	/** Description of mousePressed(MouseEvent e)
	*
	* Function listens for mouse events
	* @param e	Mouse event listener
	* @return	void
	*/
	public void mousePressed (MouseEvent e)
  	{
    		if(!guessMode){
  	  		if ((allowUserInput)){
  	    			aTable.swapSquareValue(e.getX(), e.getY());
   	    			repaint();
   	  		}
    		}
    		else{
      			System.out.println("guessed row = " + e.getY() + " guessed col = " + e.getX());
      			if(aTable.checkGuess(e.getX(), e.getY())){
				sounds[3].play();
        			int n = JOptionPane.showConfirmDialog(null, "Excellent!! Would you like to progress to next level?",
                                              "Correct!!!", JOptionPane.YES_NO_OPTION);
        			if (n == JOptionPane.YES_OPTION) {
          				guessMode = false;
          				this.add(BtnStart);
          				validate();
          				//size++;
        			}
        		else{
          			JOptionPane.showMessageDialog(null, "Thanks for playing!", "alert", JOptionPane.INFORMATION_MESSAGE);
        		}
      		}
      		else{
        		if(counter > 2)
          			counter = 0;
        		sounds[counter].play();
        		JOptionPane.showMessageDialog(null, "Nope");
        		counter++;
      		}

      		//repaint();
    	}

  }

  //set of mouse event listeners
  public void mouseClicked (MouseEvent e) {}
  public void mouseEntered (MouseEvent e) {}
  public void mouseReleased (MouseEvent e) {}
  public void mouseExited (MouseEvent e) {}

	/** Description of actionPerformed(e)
	*
	* Function 	listens for mouse actions
	* @param e	Button action listener
	* @return	void
	*/
	public void actionPerformed(ActionEvent e)
	{
		if (e.getSource() == BtnStart) {
	 	   aTable.addRow();
		   aTable.addColumn();
		   this.remove(BtnStart);
		   this.add(randomChangeBtn);
		   validate();

		   repaint();
    		}
    		else if (e.getSource() == randomChangeBtn) {
      			aTable.randomChangeFunc();
      			repaint();
      			guessMode = true;
      			this.remove(randomChangeBtn);
      			validate();
    		}
    		else if(e.getSource() == startBtn){
      			this.remove(startBtn);
		  	this.add(BtnStart);
		  	validate();
		  	repaint();
    		}

		allowUserInput = true;
		System.out.println(aTable.toString());
	}

}


